home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / components / dhDownloadProcessor.js < prev    next >
Text File  |  2010-01-15  |  5KB  |  165 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. /**
  6.  * Constants.
  7.  */
  8.  
  9. const NS_DLPROC_CID = Components.ID("{1f5c8528-c5b5-4b03-be0d-c8948028d9e6}");
  10. const NS_DLPROC_PROG_ID = "@downloadhelper.net/download-processor;1";
  11. const DHNS = "http://downloadhelper.net/1.0#";
  12.  
  13. var Util=null;
  14.  
  15. /**
  16. * Object constructor
  17. */
  18. function DLProc() {
  19.     try {
  20.         //dump("[DLProc] constructor\n");
  21.         this.helper=new DLProcHelper();
  22.         this.core=Components.classes["@downloadhelper.net/core;1"].
  23.             getService(Components.interfaces.dhICore);
  24.         this.core.registerProcessor(this);
  25.     } catch(e) {
  26.         dump("[DLProc] !!! constructor: "+e+"\n");
  27.     }
  28. }
  29.  
  30. DLProc.prototype = {
  31.     get name() { return "download"; },
  32.     get provider() { return "DownloadHelper"; },
  33.     get title() { return Util.getText("processor.download.title"); },
  34.     get description() { return Util.getText("processor.download.description"); },
  35.     get enabled() { return true; },
  36. }
  37.  
  38. DLProc.prototype.canHandle=function(desc) {
  39.     //dump("[DLProc] canHandle()\n");
  40.     return this.helper.canHandle(desc);
  41. }
  42.  
  43. DLProc.prototype.requireDownload=function(desc) {
  44.     //dump("[DLProc] requireDownload()\n");
  45.     return this.helper.requireDownload(desc);
  46. }
  47.  
  48. DLProc.prototype.preDownload=function(desc) {
  49.     //dump("[DLProc] preDownload()\n");
  50.     return this.helper.preDownload(desc,true,false);
  51. }
  52.  
  53. DLProc.prototype.handle=function(desc) {
  54.     //dump("[DLProc] handle()\n");
  55.     this.helper.handle(desc,true);
  56. }
  57.  
  58. DLProc.prototype.QueryInterface = function(iid) {
  59.     //dump("[DLProc] QueryInterface("+iid+")\n");
  60.     if(
  61.         iid.equals(Components.interfaces.dhIProcessor) ||
  62.         iid.equals(Components.interfaces.nsISupports)
  63.     ) {
  64.         return this;
  65.     }
  66.     throw Components.results.NS_ERROR_NO_INTERFACE;
  67. }
  68.  
  69. var vDLProcModule = {
  70.     firstTime: true,
  71.     
  72.     /*
  73.      * RegisterSelf is called at registration time (component installation
  74.      * or the only-until-release startup autoregistration) and is responsible
  75.      * for notifying the component manager of all components implemented in
  76.      * this module.  The fileSpec, location and type parameters are mostly
  77.      * opaque, and should be passed on to the registerComponent call
  78.      * unmolested.
  79.      */
  80.     registerSelf: function (compMgr, fileSpec, location, type) {
  81.  
  82.         if (this.firstTime) {
  83.             this.firstTime = false;
  84.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  85.         }
  86.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  87.         compMgr.registerFactoryLocation(NS_DLPROC_CID,
  88.                                         "DLProc",
  89.                                         NS_DLPROC_PROG_ID, 
  90.                                         fileSpec,
  91.                                         location,
  92.                                         type);
  93.     },
  94.  
  95.     unregisterSelf: function(compMgr, fileSpec, location) {
  96.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  97.         compMgr.unregisterFactoryLocation(NS_DH_DLPROC_CID, fileSpec);
  98.     },
  99.  
  100.     /*
  101.      * The GetClassObject method is responsible for producing Factory and
  102.      * SingletonFactory objects (the latter are specialized for services).
  103.      */
  104.     getClassObject: function (compMgr, cid, iid) {
  105.         if (!cid.equals(NS_DLPROC_CID)) {
  106.             throw Components.results.NS_ERROR_NO_INTERFACE;
  107.         }
  108.  
  109.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  110.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  111.         }
  112.  
  113.         return this.vDLProcFactory;
  114.     },
  115.  
  116.     /* factory object */
  117.     vDLProcFactory: {
  118.         /*
  119.          * Construct an instance of the interface specified by iid, possibly
  120.          * aggregating it with the provided outer.  (If you don't know what
  121.          * aggregation is all about, you don't need to.  It reduces even the
  122.          * mightiest of XPCOM warriors to snivelling cowards.)
  123.          */
  124.         createInstance: function (outer, iid) {
  125.             if (outer != null) {
  126.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  127.             }
  128.     
  129.             if(Util==null) {
  130.                 Util=Components.classes["@downloadhelper.net/util-service;1"]
  131.                     .getService(Components.interfaces.dhIUtilService);
  132.                 try {
  133.                     var jsLoader=Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  134.                         .getService(Components.interfaces.mozIJSSubScriptLoader);
  135.                     jsLoader.loadSubScript("chrome://dwhelper/content/dlproc-helper.js");
  136.                 } catch(e) {
  137.                     dump("!!! [dhDownloadProcessor] createInstance: "+e+"\n");
  138.                 }
  139.             }
  140.  
  141.             return new DLProc().QueryInterface(iid);
  142.         }
  143.     },
  144.  
  145.     /*
  146.      * The canUnload method signals that the component is about to be unloaded.
  147.      * C++ components can return false to indicate that they don't wish to be
  148.      * unloaded, but the return value from JS components' canUnload is ignored:
  149.      * mark-and-sweep will keep everything around until it's no longer in use,
  150.      * making unconditional ``unload'' safe.
  151.      *
  152.      * You still need to provide a (likely useless) canUnload method, though:
  153.      * it's part of the nsIModule interface contract, and the JS loader _will_
  154.      * call it.
  155.      */
  156.     canUnload: function(compMgr) {
  157.         return true;
  158.     }
  159. };
  160.  
  161. function NSGetModule(compMgr, fileSpec) {
  162.     return vDLProcModule;
  163. }
  164.  
  165.